package com.danskin.wechat.webapi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.danskin.wechat.entity.CustInfoEx;
import com.danskin.wechat.entity.CustInfoExView;
import com.danskin.wechat.entity.CustOrderEvaluate;
import com.danskin.wechat.entity.WxFansInfo;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.CustInfoExService;
import com.danskin.wechat.service.CustInfoService;
import com.danskin.wechat.service.CustOrderEvaluateService;
import com.danskin.wechat.service.GiftService;
import com.danskin.wechat.service.WxFansInfoService;
import com.danskin.wechat.service.WxMediaPlatformService;
import com.danskin.wechat.util.JsonUtils;
import com.danskin.wechat.util.Tools;

@Controller
@RequestMapping("api/order")
public class OrderEvaluateController extends BaseApiController {

	@Autowired
	private CustOrderEvaluateService orderEvaluateService;

	@Autowired
	private WxMediaPlatformService mpService;

	/*@Autowired
	private WxFansInfoService fansInfoService;*/

	@Autowired
	private GiftService giftService;

	@Autowired
	private CustInfoService custInfoService;

	@Autowired
	private CustInfoExService custInfoExService;

	/**
	 * 
	 * @param custOrderEvaluate
	 * @param appId
	 * @return
	 */
	@RequestMapping(value = "/saveOrederEvaluate/{appId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveOrederEvaluate(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId) {
		AccessResponse<Object> access = new AccessResponse<Object>();
		if (!map.containsKey("openId")) {
			access.setErrorCode("1");
			access.setErrorMsg("openId不能为空");
			access.setData("");
		} else if (!map.containsKey("orderNo")||(null==map.get("orderNo"))) {
			access.setErrorCode("1");
			access.setErrorMsg("订单号不能为空");
			access.setData("");
		} else if (!map.containsKey("storeHealth")) {
			access.setErrorCode("1");
			access.setErrorMsg("店铺卫生不能为空");
			access.setData("");
		} else if (!map.containsKey("prodCharacter")) {
			access.setErrorCode("1");
			access.setErrorMsg("产品特点不能为空");
			access.setData("");
		} else if (!map.containsKey("prodComfort")) {
			access.setErrorCode("1");
			access.setErrorMsg("产品舒适度不能为空");
			access.setData("");
		} else if (!map.containsKey("mid")) {
			access.setErrorCode("1");
			access.setErrorMsg("pos会员编号");
			access.setData("");
		} else if (!map.containsKey("brandId")) {
			access.setErrorCode("1");
			access.setErrorMsg("品牌ID不能为空");
			access.setData("");
		} else {
			String openId = String.valueOf(map.get("openId"));
			Map<String, Object> map2 = custInfoService.queryByOpenId(openId);
			if (null!=map2) {// 判断扫码用户 是否该系统会员
				String memberId = String.valueOf(map2.get("memberId"));
				String brandId = String.valueOf(map.get("brandId"));
				String mid = String.valueOf(map.get("mid"));
				if (StringUtils.isNotBlank(mid)) {// 是系统会员
					CustInfoExView custInfoEx = custInfoExService.getCustInfoEx(brandId, mid);
					if (null != custInfoEx) {// 数据库中 找不到此会员信息
						if (memberId.equals(custInfoEx.getMemberId())) {// 判断下单客户和评价客户是不是一个人
							WxMediaPlatform mediaPlatform = mpService.getWxMediaPlatformByAppId(appId);
							map.put("mpId", mediaPlatform.getId());
							
							//WxFansInfo wxFansInfo = fansInfoService.getWxFansInfoByOpenId(openId);
							CustInfoEx ex = custInfoExService.queryCustInfoExById(openId);
							map.put("custExId", ex.getId());
							if(StringUtils.isBlank(String.valueOf(map.get("isBuyproduct")))){
								map.remove("isBuyproduct");
							}
							if(StringUtils.isBlank(String.valueOf(map.get("isComeback")))){
								map.remove("isComeback");
							}
							CustOrderEvaluate custOrderEvaluate = Tools.toBean(map, CustOrderEvaluate.class);
							List<CustOrderEvaluate> orderAndCustList = orderEvaluateService
									.selectByOrderAndCust(custOrderEvaluate);// 判断数据库是否有记录
																				// 该订单是否已经被评价
							if (orderAndCustList.size() > 0) {
								access.setErrorCode("1");
								access.setErrorMsg("该订单已经被评价");
								access.setData("");
							} else {
								int i = orderEvaluateService.saveCustOrderEvaluateSelective(custOrderEvaluate);// 保存订单评价
																												// 同时需要更新订单表
																												// 中的状态
								if (i > 0) {
									// 调用感恩礼接口 接口开始
									AccessResponse<String> validate = giftService.validateGetActivityPrize(openId, 2);
									if (StringUtils.equals(validate.getErrorCode(), "-1")) {// 验证不通过,直接返回提示信息
										validate.setErrorCode("1");
										return JsonUtils.objectToJson(validate);
									} else {// 验证通过，执行领取奖品
										Object resultObject = giftService.getActivityPrize(openId, 2, mediaPlatform);
										if (resultObject == null) {
											access.setErrorCode("1");
											access.setErrorMsg("活动奖品领取失败");
											access.setData("");
										} else {
											access.setErrorCode("0");
											access.setErrorMsg("活动奖品领取成功");
											access.setData(resultObject);
										}
									}

								} else {
									access.setErrorCode("1");
									access.setErrorMsg("保存订单评价失败");
									access.setData("");
								}
							}
						} else {
							access.setErrorCode("1");
							access.setErrorMsg("订单客户与扫码用户不匹配");
							access.setData("");
						}
					} else {
						access.setErrorCode("1");
						access.setErrorMsg("无法获取订单客户信息或订单客户与扫码用户不匹配");
						access.setData("");
					}
				} else {// 非系统会员
					WxMediaPlatform mediaPlatform = this.getMPWithAccessToken(appId);// mpService.getWxMediaPlatformByAppId(appId);
					map.put("mpId", mediaPlatform.getId());
					CustInfoEx ex = custInfoExService.queryCustInfoExById(openId);
					//WxFansInfo wxFansInfo = fansInfoService.getWxFansInfoByOpenId(openId);
					map.put("custExId", ex.getId());
					CustOrderEvaluate custOrderEvaluate = Tools.toBean(map, CustOrderEvaluate.class);
					List<CustOrderEvaluate> orderAndCustList = orderEvaluateService
							.selectByOrderAndCust(custOrderEvaluate);// 判断数据库是否有记录
																		// 该订单是否已经被评价
					if (orderAndCustList.size() > 0) {
						access.setErrorCode("1");
						access.setErrorMsg("该订单已经被评价");
						access.setData("");
					} else {
						int i = orderEvaluateService.saveCustOrderEvaluateSelective(custOrderEvaluate);// 保存订单评价
																										// 同时需要更新订单表
																										// 中的状态
						if (i > 0) {
							// 调用感恩礼接口 接口开始
							AccessResponse<String> validate = giftService.validateGetActivityPrize(openId, 2);
							if (StringUtils.equals(validate.getErrorCode(), "-1")) {// 验证不通过,直接返回提示信息
								validate.setErrorCode("1");
								return JsonUtils.objectToJson(validate);
							} else {// 验证通过，执行领取奖品
								Object resultObject = giftService.getActivityPrize(openId, 2, mediaPlatform);
								if (resultObject == null) {
									access.setErrorCode("1");
									access.setErrorMsg("活动奖品领取失败");
									access.setData("");
								} else {
									access.setErrorCode("0");
									access.setErrorMsg("活动奖品领取成功");
									access.setData(resultObject);
								}
							}
						} else {
							access.setErrorCode("1");
							access.setErrorMsg("保存订单评价失败");
							access.setData("");
						}
					}
				}
			} else {
				access.setErrorCode("1");
				access.setErrorMsg("无法获取该用户信息，请先注册会员");
				access.setData("");
			}

		}

		return JsonUtils.objectToJson(access);
	}
	
	
	@Test
	public void test(){
		Map<String,Object> map=new HashMap<String, Object>();
	System.out.println(String.valueOf(map.get("orderNo")));
	}

}
